import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter_jpf_debug/log.dart';

class DioRequestInterceptor extends InterceptorsWrapper {

  String requestOptionsInfo(RequestOptions options){
    return "\r\n【请求url】${options.baseUrl}${options.path}\r\n"
        "【headers】${jsonEncode(options.headers)}\r\n"
        "【请求参数】${ options.queryParameters==null ?'':jsonEncode( options.queryParameters)}"
        "${ options.data == null?'': options.data is Map? jsonEncode(options.data):options.data.toString()}";
  }

  @override
  Future onRequest(RequestOptions options) {
    // print(requestOptionsInfo(options));
    return super.onRequest(options);
  }

  @override
  Future onResponse(Response response) {
    RequestOptions options = response.request;
    String msg = "${requestOptionsInfo(options)}\r\n"
        "【返回】${response.data
    }";
    Log.D(msg);
    return super.onResponse(response);
  }

  @override
  Future onError(DioError err) {
    RequestOptions options = err.request;
    Log.Er("${requestOptionsInfo(options)}\r\n"
        "${err.toString()}"
        "${err.response?.toString()}".replaceAll("\n", '\n'));
    return super.onError(err);
  }
}
